Ext.define('PJ.view.messagemanagement.Edit', {
	extend: 'AM.default.DefaultWindowForm',
	alias: ['widget.msgEdit'],
	requires: ['AM.default.DefaultDateField', 
	           'AM.default.DateTime',
	           'AM.default.jsonp.DefaultSubmitJsonp',
	           'AM.default.DefaultDateField',
	           'PJ.view.messagemanagement.PreReceiverList',
	           'PJ.view.messagemanagement.ReceiverFrame'],

	modal: true,// 开启不允许点击其他组件
	frame: true,
	notEdit: true,
	layout: 'form',
	isSame: false,
	autoHeight: true,

	initComponent: function(){
    	var me = this;
    	var bt = '<span style="color:red;">*</span>';
    	var flagStr = me.flagStr;
    	var newsTypeCode = {
            	xtype: 'combo',
            	fieldLabel: bt + '通知类型',
                name: 'newsTypeCode',
                allowBlank: false,
                queryMode: 'local',
                store: Ext.create("Ext.data.ArrayStore", {
					fields: ['value', 'text'],
					data: [["xxlx_pxb", '培训班']]
				}),
				valueField: 'value',
				displayField: 'text',
                editable: false,
                defaultActive: false,
                emptyText: '请选择',
                readOnly :true,
        		value: "xxlx_pxb",
    			columnWidth:1
//    			,
//    			handler: function(button){
//    				debugger;
//    			},
//    			listeners: {
//        			click: function(g, clm){
//        				debugger;
//        			}
//                }
            };
    	if(flagStr == "msg"){
    		newsTypeCode = {
                	xtype: 'combo',
                	fieldLabel: bt + '通知类型',
                    name: 'newsTypeCode',
                    allowBlank: false,
                    queryMode: 'local',
                    listeners: {//监听   
                        render : function(combo) {//渲染
                            combo.getStore().on("load", function(s, r, o) {   
                                combo.setValue(r[0].get('value'));//第一个值   
                            });   
                        },
    					beforeselect:function(g,clm){
    	    				var  baseForm = this.up('form').getForm();
    	    				var receiveUserName=baseForm.findField('receiveUserName').value;
    						if(receiveUserName){
    							Ext.Msg.alert('通知', "已选择通知接收人，请勿随意更换!");
    							return false;
    						}
    					}
                    },
                    store: Ext.create("Ext.data.ArrayStore", {
                        singleton: true,
                        fields: [{
                            name: 'value',
                            mapping: "dataCode",
                            type: 'string'
                        }, {
                            name: 'text',
                            mapping: "dataName",
                            type: 'string'
                        }],
                        proxy: {
                            type: 'ajax',
                            url: me.grid.store.editMessageTypeUrl + '?typeCode=txlb',
                            reader: Ext.create('Ext.data.reader.Json', {
                                type: 'json',
                                root: 'data',
                                successProperty: 'success'
                            })
                        },
                        autoLoad: true
                    }),
                    valueField: 'value',
                    displayField: 'text',
                    editable: false,
                    defaultActive: false,
                    emptyText: '请选择',
                    readOnly:me.flagStr=="class"?true:false,
//                    readOnly :true,
//            		value:me.flagStr=="class"?"xxlx_pxb":"",
        			columnWidth:1
                };
    	}
    	me.items= [{
    		defaults: {
    			labelAlign: 'right',
    			labelWidth: 80,
    			columnWidth: .5,
    			padding:"5 0 0 0",
    			readOnly:me.isView
    		},
    		border: false,
    		xtype: "container",
    		width: 480,
    		layout: 'column',
    		items: [{
            	xtype: 'textfield',
                fieldLabel: /*bt+*/'接收人',
                name: 'receiveUserName',
//	            allowBlank: false,
	            readOnly:true,
	            columnWidth: .92
            },{
    			xtype:"button",
    			columnWidth:.08,
    			margin:"5 0 0 10",
    			padding:"3 3 3 3",
    			iconCls:"planPersonIcon",
    			tooltip: {
    				title: "选择"
    			},
    			hidden: me.isView,
    			handler: function(button){
    				var  baseForm = this.up('form').getForm();
    				var code=baseForm.findField('newsTypeCode').value;
    				if(code){
        				var receiveUserIds = baseForm.findField('receiveUserId');
        				var receiveUserNames = baseForm.findField('receiveUserName');
        				var loginIds = baseForm.findField('loginIds');
        				var deptNames = baseForm.findField('deptNames');
        				Ext.create("PJ.view.messagemanagement.WindowFrame", {
                            title: "接收人列表",
                            edit:this,
                            receiveUserNames:receiveUserNames,
                            receiveUserIds:receiveUserIds,
                            userType:code,
                            loginIds:loginIds,
                            deptNames:deptNames
                        }).show();
    				}else
    					Ext.Msg.alert('通知', "请先选择通知类型!");
    			}
    		},{
            	xtype: 'textfield',
                fieldLabel: bt+'通知标题',
                name: 'newsTitle',
	            allowBlank: false,
	            readOnly:false,
	            columnWidth: 1
            },newsTypeCode,{
            	xtype: 'textfield',
                fieldLabel: bt+ "培训班名称",
                name: 'defaultValue',
                hidden:me.flagStr=="class"?false:true,
	            readOnly:true,
	            columnWidth:1
            },{
    			xtype: 'textarea',
    			fieldLabel: bt+'通知内容',
    			name: 'newsValue',
    			allowBlank: false,
    			height: 100,
    			maxLength: 200,
    			columnWidth:1
    		},{
            	xtype:"filefield",
            	fieldLabel: "请选择文件",
            	margin: '5 0 0 0',
            	labelWidth: 75,
				labelAlign:'right',
            	columnWidth: 1,
            	name: "annexName",
            	allowBlank: true,
                emptyText:"请选择文件",
                buttonText: '',
                buttonConfig: {
                iconCls: 'updateIco'
                },
                validator: function(value){
                	if(value.length!=0){
                		var arr = value.split('.');
                		if(arr[arr.length-1] == 'xls' || arr[arr.length-1] == 'xlsx' || arr[arr.length-1] == 'txt' || arr[arr.length-1] == 'jpg' || arr[arr.length-1] == 'png' || arr[arr.length-1] == 'ppt' || arr[arr.length-1] == 'pptx' || arr[arr.length-1] == 'pdf' || arr[arr.length-1] == 'doc' || arr[arr.length-1] == 'docx' || arr[arr.length-1] == 'rar' || arr[arr.length-1] == 'zip'){
                			return true;
                		}else {
                			return '您上传的文件格式不正确！';
                		}
                	}else{
                		return true;
                	}
                }
            },{
    			xtype:'hidden',
    			name:'receiveUserId'
    		},{
    			xtype:'hidden',
    			name:'loginIds'
    		},{
    			xtype:'hidden',
    			name:'deptNames'
    		}]
    	}];
    	this.buttons = [{
            text: '确定',
            handler: function(button, event){
                var win = button.up('window');
                win.sub();
            }
        }, {
            text: '取消',
            scope: this,
            handler: this.close
        }];
		this.callParent(arguments);
	},
    sub:function(){
		var win = this;
		var form = win.down('form');
		var baseForm = form.getForm();
		var newsTypeCode = baseForm.findField("newsTypeCode").getValue();
		/*if(newsTypeCode=="xxlx_pxb"){
			Ext.Msg.alert('提醒', "培训班类型只能在培训班管理中进行发送！请重新选择通知类型！", function(){
				baseForm.findField("newsTypeCode").setValue("");
			});
		}else{*/
			if (!baseForm.isValid()) {
				return;
			}
			var url =win.grid.store.addMsgUrl;
			if(gj_onlineLearning_constant.loadType == "jsonp"){
				baseForm.doAction('submitJsonp',{
					url: url,
					params:{
						searchClassID:win.classId
					},
					success: function(f, action){
						if (action.result.success) {
							Ext.Msg.alert('通知', "操作成功!", function(){
								var gridPanel = win.grid;
								var store = gridPanel.getStore();
								store.reload();
								win.close();
							});
						}else{
							Ext.Msg.alert('通知', action.result.msg||"系统出错,请联系管理员!", function(){
								win.close();
							});
						}
					},
					failure: function(f, action){
						if (action.result) {
							Ext.Msg.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
						else {
							Ext.Msg.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
					}
				});
			}else{
				baseForm.submit({
					url: url,
					success: function(f, action){
						if (action.result.success) {
							Ext.Msg.alert('通知', "操作成功!", function(){
								var gridPanel = win.grid;
								var store = gridPanel.getStore();
								store.reload();
								win.close();
							});
						}else{
							Ext.Msg.alert('通知', action.result.msg||"系统出错,请联系管理员!", function(){
								win.close();
							});
						}
					},
					failure: function(f, action){
						if (action.result) {
							Ext.Msg.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
						else {
							Ext.Msg.alert('错误', "系统出错,请联系管理员!");
						}
					}
				});
			}
//		}
	
    }
});